package lar.system.network;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import lar.system.network.msgParser.Strings;
import lar.system.opengl.GLColor;

public class TCPConnection extends Connection 
{
	SocketChannel sChannel;
	
	private static ByteBuffer outBuf=null;
	
	public TCPConnection(SocketChannel sc) 
	{
		sChannel = sc;
	}
	
	public SocketChannel getSocketChannel()
	{
		return sChannel;
	}

	public void shutDown()
	{
		if(sChannel == null) return;
		try 
		{
			sChannel.close();
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		sChannel = null;
	}
	
	public void send(String msg) 
	{
		if(sChannel == null) return;
		msg += Strings.FINALIZER;
		try 
		{
			outBuf = ByteBuffer.wrap(msg.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e1) {}
		while (outBuf.remaining() > 0) 
		{
			try 
			{
				sChannel.write(outBuf);
			} catch(IOException e) { e.printStackTrace(); }
		}
	}

}
